/*
 * DBHandler.java
 *
 * Created on 18 settembre 2005, 12.09
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package database;

import java.sql.*;

/**
 *
 * @author Paolo Scalia
 */
public class DBHandler {
    private static DBHandler instance;
    protected Connection connection;
    
    protected String dbHost = "localhost";
    protected String dbName = "rubikscube";
    protected String dbUser = "root";
    protected String dbPort = "1521";
    protected String dbPassword = "";
    protected String noPoolConnectionString = "jdbc:mysql://" + dbHost + "/" + dbName;// + "?user=" + dbUser + "&password=" + dbPassword;
    
    protected static final boolean USEPOOL = false;
    
    private DBHandler() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(noPoolConnectionString, dbUser, dbPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public synchronized static DBHandler getInstance() {
        if (instance == null) instance = new DBHandler();
        return instance;
    }
    
    public void insertPattern(String init, String movesList) {
        PreparedStatement statement = null;
        
        String query = "INSERT INTO LPFirstStep (init, moveslist) VALUES ";
        query += "(?, ?)";
        
        try {
            statement = connection.prepareStatement(query);
            statement.setString(1, init);
            statement.setString(2, movesList);
            statement.executeUpdate();
        } catch (Exception e) {
        } finally {
            try {
                statement.close();
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }
    
}
